package cn.ronghuanet.car.controller;

import cn.ronghuanet.base.util.LoginContext;
import cn.ronghuanet.car.service.ICarService;
import cn.ronghuanet.car.domain.Car;
import cn.ronghuanet.car.query.CarQuery;
import cn.ronghuanet.base.util.AjaxResult;
import cn.ronghuanet.base.util.PageInfo;
import cn.ronghuanet.org.domain.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/car")
public class CarController {
    @Autowired
    public ICarService carService;


    /**
     * 保存和修改公用的
     * @param car  传递的实体
     * @return Ajaxresult转换结果
     */
    @PostMapping
    public AjaxResult addOrUpdate(@RequestBody Car car, HttpServletRequest request){
        Employee loginUser = LoginContext.getLoginUser(request);
        if( car.getId()!=null)
            carService.update(car);
        else
            carService.insert(car,loginUser);
        return AjaxResult.success();
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        carService.delete(id);
        return AjaxResult.success();
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id)
    {
        Car car = carService.selectById(id);
        return AjaxResult.success(car);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult list(){
        List< Car> list = carService.selectAll();
        return AjaxResult.success(list);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PutMapping
    public AjaxResult json(@RequestBody CarQuery query)
    {
        PageInfo<Car> pageList = carService.queryPage(query);
        return AjaxResult.success(pageList);
    }

    /**
     * 上架
     * @param ids
     * @return
     */
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids,HttpServletRequest request){
        Employee loginUser = LoginContext.getLoginUser(request);
        String msg = carService.onsale(ids, loginUser);
        return AjaxResult.successMsg(msg);
    }

    /**
     * 下架
     * @param ids
     * @return
     */
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids,HttpServletRequest request){
        Employee loginUser = LoginContext.getLoginUser(request);
        carService.offsale(ids,loginUser);
        return AjaxResult.success();
    }
}
